var channel = null;

function showOPInfo(szInfo) {
	//szInfo = dateFormat(new Date(), "yyyy-MM-dd hh:mm:ss") + " " + szInfo;
	console.info(szInfo);
}


export function initHikvisionComponent(){
	console.log( "-------------- begin ")
	console.log( WebVideoCtrl );
    (function(){ 
        // 检查插件是否已经安装过
        if (-1 == WebVideoCtrl.I_CheckPluginInstall()) {
			alert("您还未安装过插件，双击开发包目录里的WebComponents.exe安装！");
			return;
        }
        
        
        // 初始化插件参数及插入插件
        WebVideoCtrl.I_InitPlugin(500, 300, {
               iWndowType: 1,
        	cbSelWnd: function (xmlDoc) {
        		var g_iWndIndex = $(xmlDoc).find("SelectWnd").eq(0).text();
        		var szInfo = "当前选择的窗口编号：" + g_iWndIndex;
        		showOPInfo(szInfo);
        	}
    	});
    })();
	var rtspvideo = document.getElementById( 'rtspvideo');

	if( -1 === WebVideoCtrl.I_InsertOBJECTPlugin( "rtspvideo" ) ) { 
		alert( "insert Video object error   ");
	}
	console.log( "insert -----------------   rtsp video ");

	// 检查插件是否最新
	if ( -1 == WebVideoCtrl.I_CheckPluginVersion() ) {
		alert(" wesine -----------检测到新的插件版本，双击开发包目录里的WebComponents.exe升级！");
		return ;
	}
	console.log( "----------------------------  finish init hikvision component ----------------------");
}

// 登录
export  function login (addr) {

	console.log( "----------------- hikvision login  ", addr );
	var resolveLogin , rejectLogin;
	var promiseLogin = new Promise( function( resolve, reject ){
		resolveLogin = resolve;
		rejectLogin = reject;
	});
	var szIP = addr.ip,
		szPort = addr.port,
		szUsername = addr.username,
		szPassword = addr.password;

	if ("" == szIP || "" == szPort) {
		rejectLogin( "错误的ｉｐ和ｐｏｒｔ: ", ip , " , ", port )
		return;
	}
	console.log( "WebVideoCtrl: ", WebVideoCtrl );


	var iRet = WebVideoCtrl.I_Login(szIP, 1 , szPort, szUsername, szPassword, {
		success: function (xmlDoc) {
			showOPInfo(szIP + " 登录成功！");
			resolveLogin( addr );
		},
		error: function () {
			showOPInfo(szIP + " 登录失败！");
			rejectLogin( addr );
		}
	});

	if (-1 == iRet) {
		showOPInfo(szIP + " 已登录过！");
		resolveLogin( addr )
	}
	return promiseLogin;
}


// 开始预览
export  function playback(  ip, channel ) {
	console.log( "------------ play channel:  ", channel );

	console.log( "WebVideCtr: ", WebVideoCtrl );

    var szIP = ip,
    	iStreamType = 1,
    	iChannelID = channel,
    	bZeroChannel = false,
		sInfo = "";
		console.log( 'channel: ', channel );
    
    if ( "" == szIP ) {
    	console.error( "错误的  camera ip")
    	return;
    }
	
	console.log( "--------", WebVideoCtrl );
	var iRet = WebVideoCtrl.I_StartRealPlay( szIP, 
		{
        	iStreamType: iStreamType,
        	iChannelID: iChannelID,
			bZeroChannel: bZeroChannel
		});
    
    if ( 0 == iRet ) {
    	szInfo = "开始预览成功！";
    } else {
    	szInfo = "开始预览失败！";
    }
    showOPInfo( szIP + " " + szInfo );
}
// 获取通道
export function getChannels( ip ) {

	var resolveChannels, rejectChannels;

	promise = new Promise( function( resolve, reject ) { 
		resolveChannels = resolve;
		rejectChannels = reject;
	});

	var szIP = addr.ip,
		//oSel = $("#channels").empty(),
		nAnalogChannel = 0;

	if ("" == szIP) {
		return;
	}

	var channels = { };


	// 数字通道
	WebVideoCtrl.I_GetDigitalChannelInfo(szIP, {
		async: false,
		success: function (xmlDoc) {
			console.log( xmlDoc )
			console.log( "get channels: ", xmlDoc.innerHtml )
			var  dchannels = xmlDoc.getElementByTagName("InputProxyChannelStatus");
			console.log( "数字通道: ", oChannels );
			for( let i = 0; i < dchannels.length; i++ ) {
				var id = parseInt( dchannels[i].querySelect( 'id').innerHTML );
				channels[id] = "d";
				resolveChannels();
			}
			showOPInfo(szIP + " 获取数字通道成功！");
		},
		error: function () {
			showOPInfo(szIP + " 获取数字通道失败！");
			rejectChannels();
		}
	});
	return promise;
}


